<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Information extends Model
{
    protected $table = 'gov_informations';

    public static $catMap = [
        'GZDT'   => '122',//工作动态
        'TZGG'   => '123',//通知公告
        'JXYW'   => '124',//经信要闻
        'SNJXYW' => '125',//省内经信要闻
        'XQJXXX' => '126',//县区经信信息
        'DQGZ'   => '129',//党群工作
        'GYJJFZ' => '131',//工业经济发展
        'XXCYFZ' => '132',//信息产业发展
    ];

    public function category()
    {
        return $this->belongsTo(Category::Class,'cat_id');
    }

    public function user()
    {
        return $this->belongsTo(User::Class);
    }

    public static function getInformations($cat_id, $limit)
    {
        $category = new Category;
        $sub_cat = $category->getCatChildId($cat_id);

        if ($sub_cat) {
            return static::whereIn('cat_id', $sub_cat)->select(['id', 'title', 'created_at'])->orderBy('created_at', 'desc')->limit($limit)->get()->toArray();
        } else {
            return static::where('cat_id', $cat_id)->select(['id', 'title', 'created_at'])->orderBy('created_at', 'desc')->limit($limit)->get()->toArray();
        }

    }




}
